package net.lab1024.sa.admin.module.business.exam.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.lab1024.sa.admin.module.business.exam.dao.CoursecDao;
import net.lab1024.sa.admin.module.business.exam.domain.entity.CoursecEntity;
import net.lab1024.sa.admin.module.business.exam.domain.form.add.CoursecAddForm;
import net.lab1024.sa.admin.module.business.exam.domain.form.query.CoursecQueryForm;
import net.lab1024.sa.admin.module.business.exam.domain.form.update.CoursecUpdateForm;
import net.lab1024.sa.admin.module.business.exam.domain.vo.CoursecVO;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 生产订单与机器对应表 Service
 *
 * @Author AKITA
 * @Date 2025-06-30 15:46:01
 * @Copyright AKITA
 */

@Service
public class CoursecService {

    @Resource
    private CoursecDao coursecDao;

    /**
     * 分页查询
     */
    public PageResult<CoursecVO> queryPage(CoursecQueryForm queryForm) {
        Page<?> page = SmartPageUtil.convert2PageQuery(queryForm);
        List<CoursecVO> list = coursecDao.queryPage(page, queryForm);
        return SmartPageUtil.convert2PageResult(page, list);
    }

    /**
     * 添加
     */
    public ResponseDTO<String> add(CoursecAddForm addForm) {
        CoursecEntity coursecEntity = SmartBeanUtil.copy(addForm, CoursecEntity.class);
        coursecDao.insert(coursecEntity);
        return ResponseDTO.ok();
    }

    /**
     * 更新
     */
    public ResponseDTO<String> update(CoursecUpdateForm updateForm) {
        CoursecEntity coursecEntity = SmartBeanUtil.copy(updateForm, CoursecEntity.class);
        coursecDao.update(coursecEntity, new LambdaQueryWrapper<CoursecEntity>().eq(CoursecEntity::getDocEntry, updateForm.getDocEntry()).eq(CoursecEntity::getLineNum, updateForm.getLineNum()));
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     */
    public ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return ResponseDTO.ok();
        }

        coursecDao.deleteBatchIds(idList);
        return ResponseDTO.ok();
    }

    /**
     * 单个删除
     */
    public ResponseDTO<String> delete(Long docEntry) {
        if (null == docEntry) {
            return ResponseDTO.ok();
        }

        coursecDao.deleteById(docEntry);
        return ResponseDTO.ok();
    }
}
